﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Microsoft.ManagementConsole.SnapIns
{
    public class SelectionListView : MmcListView
    {
        public SelectionListView()
        {
        }

        protected override void OnInitialize(AsyncStatus status)
        {
            base.OnInitialize(status);

            this.Columns[0].Title = "Projects";
            this.Columns[0].SetWidth(300);

            this.Columns.Add(new MmcListViewColumn("Deadline", 200));

            this.Mode = MmcListViewMode.Report;

            this.SelectionData.EnabledStandardVerbs = StandardVerbs.Refresh;

            Refresh();
        }

        protected override void OnSelectionChanged(SyncStatus status)
        {
            if (this.SelectedNodes.Count == 0)
            {
                this.SelectionData.Clear();
            }
            else
            {
                this.SelectionData.Update(GetSelectedProjects(), this.SelectedNodes.Count > 1, null, null);
                this.SelectionData.ActionsPaneItems.Clear();
                this.SelectionData.ActionsPaneItems.Add(new Action("Show Project Detail", "Shows list of selected Projects.", -1, "ShowSelected"));
                this.SelectionData.ActionsPaneItems.Add(new Action("Add New Projects", "AddNewProjects.", -1, "AddNewProject"));

            }
        }

        protected override void OnRefresh(AsyncStatus status)
        {
            MessageBox.Show("The method or operation is not implemented.");
        }

        protected override void OnSelectionAction(Action action, AsyncStatus status)
        {
            switch ((string)action.Tag)
            {
                case "ShowSelected":
                    {
                        ShowSelected();
                        break;
                    }

                case "AddNewProject":
                    {
                        FrmAddNewProject frm = new FrmAddNewProject();

                        frm.ShowDialog();
                        break;
                    }
            }
        }

        private void ShowSelected()
        {
            MessageBox.Show("Selected Project: \n" + GetSelectedProjects());
        }

        private string GetSelectedProjects()
        {
            StringBuilder selectedProjecs = new StringBuilder();

            foreach (ResultNode resultNode in this.SelectedNodes)
            {

                selectedProjecs.Append(resultNode.DisplayName + "\n");
            }

            return selectedProjecs.ToString();
        }

        public void Refresh()
        {
            this.ResultNodes.Clear();

            string[][] projects = { new string[] {"Mesafeli", "February 14th"},
                                        new string[] {"FonSepeti", "May 5th"},
                                        new string[] {"Portföy", "April 15th"},
                                    };

            foreach (string[] project in projects)
            {
                ResultNode node = new ResultNode();
                node.DisplayName = project[0];
                node.SubItemDisplayNames.Add(project[1]);

                this.ResultNodes.Add(node);
            }
        }
    }
}
